package com.qa.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qa.pojo.Temp_WqtWeb;
import com.qa.pojo.Temp_lbt;
import com.qa.pojo.Temp_user_use;
import com.qa.pojo.Temp_zzttWeb;
import com.qa.pojo.Users;
import com.qa.service.ITempService;

@Controller
@RequestMapping("/temp")
public class TempController {
	@Resource
	private ITempService tempService;
	Logger logger = Logger.getLogger(this.getClass());

	/**
	 * 查找个人findzzttTemp模板配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/findzzttTemp")
	@ResponseBody
	public Map<String, Object> findzzttTemp(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			List<Temp_zzttWeb> listTemp_zzttwebs = tempService
					.selectzzttWebByUsersid(usersid);
			Temp_user_use t_use = tempService.selectReByUsersid(usersid);
			if (listTemp_zzttwebs != null && listTemp_zzttwebs.size() > 0) {
				resultMap.put("tag", "success");
				resultMap.put("result", listTemp_zzttwebs);
				resultMap.put("t_use", t_use);
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 查找个人findWqtTemp模板配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/findWqtTemp")
	@ResponseBody
	public Map<String, Object> findWqtTemp(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			List<Temp_WqtWeb> listTemp_wqts = tempService
					.selectWqtWebByUsersid(usersid);
			Temp_user_use t_use = tempService.selectReByUsersid(usersid);
			if (listTemp_wqts != null && listTemp_wqts.size() > 0) {
				resultMap.put("tag", "success");
				resultMap.put("result", listTemp_wqts);
				resultMap.put("t_use", t_use);
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 查找个人lbt模板配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/findLbtTemp")
	@ResponseBody
	public Map<String, Object> findLbtTemp(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			List<Temp_lbt> listTemp_lbts = tempService.selectByUsersid(usersid);
			Temp_user_use t_use = tempService.selectReByUsersid(usersid);
			if (listTemp_lbts != null && listTemp_lbts.size() > 0) {
				resultMap.put("tag", "success");
				resultMap.put("result", listTemp_lbts);
				resultMap.put("t_use", t_use);
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 删除个人模板配置及关系，为lbt提供使用
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/deleteTemp", method = RequestMethod.POST)
	@ResponseBody
	public boolean deleteTemp(HttpServletRequest request,
			HttpServletResponse response) {
		response.setStatus(200);
		int deleteTemp = 0, deleteRe = 0;
		String projectname = request.getParameter("projectname");
		int usersid = ((Users) request.getSession().getAttribute("user"))
				.getUsersid();
		int tempid = 0;
		try {
			switch (projectname) {
			case "lbt":
				tempid = Integer.valueOf(request.getParameter("tLbttempid"));
				deleteTemp = this.tempService.deleteTemp(tempid);
				deleteRe = this.tempService.deleteRe(usersid, tempid, "lbt");
				break;
			case "wqtweb":
				tempid = Integer.valueOf(request.getParameter("tWqttempid"));
				deleteTemp = this.tempService.deleteWqtTemp(tempid);
				deleteRe = this.tempService.deleteRe(usersid, tempid, "wqtweb");
				break;
			case "zzttweb":
				tempid = Integer.valueOf(request.getParameter("tZztttempid"));
				deleteTemp = this.tempService.deletezzttTemp(tempid);
				deleteRe = this.tempService.deleteRe(usersid, tempid, "zzttweb");
				break;
			default:
				break;
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		if (deleteTemp > 0 && deleteRe >= 0) {
			return true;
		}
		return false;
	}

	/**
	 * 解除个人和模板绑定
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/unbindTemp", method = RequestMethod.POST)
	@ResponseBody
	public boolean unbindTemp(HttpServletRequest request,
			HttpServletResponse response) {
		response.setStatus(200);
		int deleteRe = 0;
		try {
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			deleteRe = this.tempService.deleteRe(usersid);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		if (deleteRe >= 0) {
			return true;
		}
		return false;
	}

	/**
	 * 个人使用lbt模板
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/useTemp_lbt", method = RequestMethod.POST)
	@ResponseBody
	public boolean useTemp_lbt(HttpServletRequest request,
			HttpServletResponse response) {
		response.setStatus(200);
		int insertRe = 0, deleteRe = 0;
		String projectname = request.getParameter("projectname");
		int tempid = 0;
		try {
			switch (projectname) {
			case "lbt":
				tempid = Integer.valueOf(request.getParameter("tLbttempid"));
				break;
			case "wqtweb":
				tempid = Integer.valueOf(request.getParameter("tWqttempid"));
				break;
			case "zzttweb":
				tempid = Integer.valueOf(request.getParameter("tZztttempid"));
				break;
			default:
				break;
			}
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			deleteRe = this.tempService.deleteRe(usersid);
			Temp_user_use t_use = new Temp_user_use();
			t_use.setProjectname(projectname);
			t_use.setTempid(tempid);
			t_use.setUsersid(usersid);
			insertRe = this.tempService.insert(t_use);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		if (insertRe == 1 && deleteRe >= 0) {
			return true;
		}
		return false;
	}

	/**
	 * 个人模板配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveTemp", method = RequestMethod.POST)
	@ResponseBody
	public boolean saveTemp(HttpServletRequest request,
			HttpServletResponse response) {
		boolean isSuccess = false;
		response.setStatus(200);
		int a = 0;
		try {
			int usersid = ((Users) request.getSession().getAttribute("user"))
					.getUsersid();
			String tempName = request.getParameter("tempName");
			switch (tempName) {
			// 根据tempname来区分当前是那个项目的
			case "wqtweb":
				String wqtweburl = URLDecoder.decode(
						request.getParameter("wqtweburl"), "UTF-8");
				String userName = URLDecoder.decode(
						request.getParameter("userName"), "UTF-8");
				String userPassword = URLDecoder.decode(
						request.getParameter("userPassword"), "UTF-8");
				String v_qy_Num = URLDecoder.decode(
						request.getParameter("v_qy_Num"), "UTF-8");

				a = this.tempService.insertWqtWeb(wqtweburl, userName,
						userPassword, v_qy_Num, usersid);
				break;
			case "lbt":
				Temp_lbt temp_lbt = new Temp_lbt();
				temp_lbt.setUrl(URLDecoder.decode(request.getParameter("url"),
						"UTF-8"));
				temp_lbt.setLoginname(URLDecoder.decode(
						request.getParameter("loginName"), "UTF-8"));
				temp_lbt.setPlatform(URLDecoder.decode(
						request.getParameter("platform"), "UTF-8"));
				temp_lbt.setSysver(URLDecoder.decode(
						request.getParameter("sysver"), "UTF-8"));
				temp_lbt.setPhonemodel(URLDecoder.decode(
						request.getParameter("phonemodel"), "UTF-8"));
				temp_lbt.setPassword(URLDecoder.decode(
						request.getParameter("passWord"), "UTF-8"));
				temp_lbt.setAppver(URLDecoder.decode(
						request.getParameter("appver"), "UTF-8"));
				temp_lbt.setImsi(URLDecoder.decode(
						request.getParameter("imsi"), "UTF-8"));
				temp_lbt.setUsersid(usersid);
				a = this.tempService.insert(temp_lbt);
				break;
			case "zzttweb":
				String zzttweburl = URLDecoder.decode(
						request.getParameter("zzttweburl"), "UTF-8");
				String zzttuserName = URLDecoder.decode(
						request.getParameter("zzttuserName"), "UTF-8");
				String zzttuserPassword = URLDecoder.decode(
						request.getParameter("zzttuserPassword"), "UTF-8");
				String zzttenamesubcomp = URLDecoder.decode(
						request.getParameter("zzttenamesubcomp"), "UTF-8");

				a = this.tempService.insertzzttWeb(zzttweburl, zzttuserName,
						zzttuserPassword, zzttenamesubcomp, usersid);
				break;
			default:

				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		if (a == 1) {
			isSuccess = true;
		}
		return isSuccess;
	}
}